package com.example.wanghao.wangyixinwen.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.utils.DensityUtil;
import com.example.wanghao.wangyixinwen.view.Rotatetransformer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanghao on 2016/5/16.
 */
public class GuideActivity extends Activity {

    //private CustomViewPager mMViewPager;
    private ViewPager mMViewPager;
    //图片数组
    private int[] imageResIds = {R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
    private List<ImageView> mImages;//集合数据
    private Button mButton;
    private LinearLayout mLl_point_group;//水平线性布局用来放置默认点的容器
    private ImageView mIv_foucs;//选中点
    private int mPoint_width;//点之间的距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        // mMViewPager = (CustomViewPager) findViewById(R.id.viewpager);
        mMViewPager = (ViewPager) findViewById(R.id.viewpager);
        mButton = (Button) findViewById(R.id.button);
        mLl_point_group = (LinearLayout) findViewById(R.id.ll_point_group);
        mIv_foucs = (ImageView) findViewById(R.id.iv_foucs);

        GuideAdapter adapter = new GuideAdapter();
        mMViewPager.setAdapter(adapter);
        //mMViewPager.setPageTransformer(true,new ZoomOutPageTransformer());
        //mMViewPager.setPageTransformer(true, new DepthPageTransformer());

        mMViewPager.setPageTransformer(true,new Rotatetransformer());

        // mMViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        mMViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           //当页面滑动的时候,实时调用
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //当手指滑动的时候,实时设置选中点红点的x轴的平移
                //int translationX = （position+offset）*间距
                float translationx = (position + positionOffset)*mPoint_width;
                mIv_foucs.setTranslationX(translationx);

            }

            //当页面选中的时候调用
            @Override
            public void onPageSelected(int position) {

                if (position == mImages.size() - 1){
                    mButton.setVisibility(View.VISIBLE);
                }else{
                    mButton.setVisibility(View.INVISIBLE);
                }
            }

            //当滑动状态改变时调用
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //根据引导界面来动态创建默认点
        for (int i = 0; i < imageResIds.length;i++ ){
            ImageView point = new ImageView(GuideActivity.this);
            LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(DensityUtil.dip2px(GuideActivity.this, 10),
                    DensityUtil.dip2px(GuideActivity.this, 10));
            //设置左边距
            if (i != 0){
                Params.leftMargin = DensityUtil.dip2px(GuideActivity.this, 10);
            }
            point.setLayoutParams(Params);
            point.setImageResource(R.drawable.dot_normal);
            mLl_point_group.addView(point);
        }



        mIv_foucs.postDelayed(new Runnable() {



            @Override
            public void run() {
                //计算点之间的距离
                mPoint_width = mLl_point_group.getChildAt(1).getLeft() -
                        mLl_point_group.getChildAt(0).getLeft();
                System.out.println("point_width" + mPoint_width);
            }
        },20);
    }

    private void initData() {
        mImages = new ArrayList<>();
        for (int i = 0;i<imageResIds.length;i++){
            ImageView iv = new ImageView(GuideActivity.this);
            //iv.setImageResource(imageResIds[i]);
            iv.setBackgroundResource(imageResIds[i]);
            mImages.add(iv);
        }
    }

    private class GuideAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = mImages.get(position);
            container.addView(iv);
            // mMViewPager.addChildView(iv,position);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
            // mMViewPager.removeChildView(position);
        }
    }
    public void goMain(View view){
        Intent intent = new Intent(GuideActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
